import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
        {
          path: '/',
          name: 'login',
          component: () => import('../views/Login')
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('../views/Home'),
          redirect: '/systemHome',
          children:[
              { path:'/systemHome',name:'systemHome', component:() => import('../ components/systemHome') },
              { path:'/logisticsSettings',name:'logisticsSettings', component:() => import('../ components/logisticsSettings') },
              { path:'/orderReport',name:'orderReport', component:() => import('../ components/orderReport') },
              { path:'/consignedMail',name:'consignedMail', component:() => import('../ components/consignedMail') },
              { path:'/seeMail',name:'seeMail', component:() => import('../ components/seeMail') },
              { path:'/editMail',name:'editMail', component:() => import('../ components/editMail') },
              { path:'/mailingList',name:'mailingList', component:() => import('../ components/mailingList') },
              { path:'/deliveryManagement',name:'deliveryManagement', component:() => import('../ components/deliveryManagement') },
              { path:'/warehousingManagement',name:'warehousingManagement', component:() => import('../ components/warehousingManagement') },
              { path:'/issueManagement',name:'issueManagement', component:() => import('../ components/issueManagement') },
              { path:'/productList',name:'productList', component:() => import('../ components/productList') },
              { path:'/commodityPriceBook',name:'commodityPriceBook', component:() => import('../ components/commodityPriceBook') },
              { path:'/commodityAttribute',name:'commodityAttribute', component:() => import('../ components/commodityAttribute') },
          ]
        }
    ]

})

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next()
  } else {
    const token = window.sessionStorage.getItem('token')
    if (!token) {
      next('/')
    } else {
      next()
    }
  }
})
export default router
